package Homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*作业1：
随机生成50个男生姓名(年龄 20-25)，50个女生姓名(18-22) 格式:
金胤霆-男-23
邓希勤-女-18
龙思安-女-22
汪兆毅-男-25
赵希勤-男-22
沈伯特-女-22*/
public class work07 {
    public static void main(String[] args) throws Exception {
        String xingStr="https://www.jimingbao.com/90981.html";
        //男生
        String mingMan="http://www.haoming8.cn/mingzi/59965.html";
        //女生
        String mingWoman="http://www.haoming8.cn/mingzi/59969.html";

        String webCrawlerXing = webCrawler(xingStr);
        String webCrawlerMingM = webCrawler(mingMan);
        String webCrawlerMingW = webCrawler(mingWoman);
//		System.out.println(webCrawlerXing);


        //通过正则表达式，把其中符号要求的数据获取出来
        ArrayList<String> dataXing = getData(webCrawlerXing,"(.、){2}");
        ArrayList<String> dataMingMan = getData(webCrawlerMingM,"(..、){4}..");
        ArrayList<String> dataMingWoman = getData(webCrawlerMingW,"(..、){4}..");

        System.out.println(dataXing);
        System.out.println(dataMingMan);
        System.out.println(dataMingWoman);
        //处理姓氏
        ArrayList<String> xingList=new ArrayList();
        for (String xing : dataXing) {
            //李、王、
            String[] split = xing.split("、");
            for(int i=0;i<split.length;i++){
                xingList.add(split[i]);
            }
        }
        System.out.println(xingList);
        //处理男生名字
        ArrayList<String> mingListMan=new ArrayList();
        for (String ming : dataMingMan) {
            //哲睿、益昌、夏明、胜才、浚龙
            String[] split = ming.split("、");

            for(int i=0;i<split.length;i++){
                mingListMan.add(split[i]);
            }
        }
        System.out.println(mingListMan);
        //处理女生名字
        ArrayList<String> mingListWoman=new ArrayList();
        for (String ming : dataMingWoman){
            String[] spl = ming.split("、");
            for (int i = 0;i<spl.length;i++){
                if (mingListWoman.size() <= 60){
                    mingListWoman.add(spl[i]);
                }
            }
        }
        System.out.println(mingListWoman);

        //自由生成名字
        ArrayList<String> infos = getInfos(xingList,mingListMan,50);
        ArrayList<String> infosW = getInfos(xingList,mingListWoman,50);
        System.out.println(infos);

        //生产指定区间内随机数的方法：int randNumber = rand.nextInt(MAX - MIN + 1) + MIN;
        Random rm = new Random();
        //写出数据
        BufferedWriter bw=new BufferedWriter(new FileWriter("names.txt"));
        for (String string : infos) {
            int age = rm.nextInt(6) + 20;
            bw.write(string+"-男-"+age);
            bw.newLine();
        }
        for (String string : infosW){
            int age = rm.nextInt(5) + 18;
            bw.write(string+"-女-"+age);
            bw.newLine();
        }
        bw.close();

        //在你生成的文件中，随机点名名字，在控制台打印一行
        shuffle();
    }
    private static void shuffle() throws Exception {
        // TODO Auto-generated method stub
        BufferedReader reader = new BufferedReader(new FileReader("names.txt"));
        ArrayList<String> list = new ArrayList<String>();
        String line = null;
        while ((line=reader.readLine())!=null){
            list.add(line);
        }
        Collections.shuffle(list);
        System.out.println("随机点名："+list.get(0));
        reader.close();
    }
    private static ArrayList<String> getInfos(ArrayList<String> xingList, ArrayList<String> mingList, int count) {
        HashSet<String> set=new HashSet<>();
        while(true){
            if(set.size()==count){
                break;
            }
            //随机
            Collections.shuffle(mingList);
            Collections.shuffle(xingList);
            set.add(xingList.get(0)+mingList.get(0));
        }
        ArrayList<String> list=new ArrayList<>(set);
        return list;
    }
    public static ArrayList<String> getData(String str,String regex){
        ArrayList<String> list=new ArrayList<>();
        //按照正则表达式的规则，去获取数据
        Pattern pattern=Pattern.compile(regex);
        //按照pattern规则，到str中获取数据
        Matcher matcher = pattern.matcher(str);
        while(matcher.find()){
            String group = matcher.group();
//			System.out.println(group);
            list.add(group);
        }
        return list;
    }

    /*
     * 作用：
     *  从网络中爬取数据，把数据拼接成字符串返回
     *  参数：网址
     *  返回值：爬取到的所有的数据
     */
    public static String webCrawler(String net) throws Exception{
        StringBuilder sb=new StringBuilder();
        //创建一个url对象
        URL url=new URL(net);
        //连接上这个网址 ，保证网络是畅通的
        URLConnection conn = url.openConnection();
        //读取数据
        InputStreamReader isr=new InputStreamReader(conn.getInputStream(),"utf-8");
        int ch;
        while((ch=isr.read())!=-1){
            sb.append((char)ch);
        }
        return sb.toString();
    }
}
